var baseUrl;
// var baseURL = "https://lgb24kcs.cn:9090"
let envVersion = wx.getAccountInfoSync().miniProgram.envVersion;
switch (envVersion) {
  case "develop": // 开发版
    // baseUrl = "http://testapplet.boyunxr.com"; //测试环境
    // baseUrl = "https://lgb24kcs.cn:9090"; //测试环境
    baseUrl = "https://localhost:9090"; //测试环境

    break;
  case "trial": // 体验版
    baseUrl = "https://lgb24kcs.cn:9090"; //测试环境
    break;
  case "release": // 正式版
    baseUrl = "https://lgb24kcs.cn:9090"; //正式环境
}


const httpRequest = function (url, method = 'POST', params = {}, need_token = true, header = {
  'content-type': 'application/json;charset=utf-8'
}, timeout = 60000) {

  return new Promise((resolve, reject) => {
    if (need_token == true) {
      let auth_token = wx.getStorageSync('auth_token')
      header.Authorization = 'Bearer ' + auth_token
    }
    var request_url = baseUrl + url
    wx.request({
      url: request_url,
      method: method,
      data: params,
      header: header,
      dataType: 'json',
      timeout: timeout,
      success(res) {
        switch (res.data.code) {
          case 200:
            resolve(res.data.data)
            break;
          case 401:
            // wx.clearStorageSync('auth_token')
            wx.removeStorageSync('auth_token')
            wx.removeStorageSync('userInfo')

            wx.showToast({
              title: res.data.message,
              icon: "none"
            })
            setTimeout(() => {
              wx.reLaunch({
                url: '/pages/login/login'
              })
            }, 2000);
            break;
          default:
            reject(res.data.message)
            // wx.showToast({
            //   title: res.data.message,
            //   icon: 'none',
            // })
            break
        }
      },
      fail(err) {
        // wx.showToast({
        //   title: "服务异常",
        //   icon: "none"
        // })
        reject(err)
      }
    })

  })
}
// const tips = function (title, time = 1500) {
//   wx.showToast({
//     title: title,
//     icon: 'none',
//     // image: '../../images/fail.png',
//     duration: time,
//     mask: false
//   })
// }
exports.httpRequest = httpRequest
// exports.tips = tips
exports.baseUrl = baseUrl